//
//  GameScene.cpp
//  dot
//
//  Created by zz on 2019/6/18.
//

#include "GameScene.hpp"


Scene * GameScene::createScene(){
    
    auto game = GameScene::create();
    auto scene = Scene::create();
    scene->addChild(game);
    return scene;
}
bool GameScene::init(){
    
    if(!Layer::init()){
        return false;
    }
    
    this->visibleSize = Director::getInstance()->getVisibleSize();
    
    auto bg = Sprite::create("gamebg.png");
    bg->setPosition(this->visibleSize/2);
    bg->setScale(1.2);
    this->addChild(bg);
    
    
    auto back = MenuItemImage::create("home.png","home_ac.png",CC_CALLBACK_0(GameScene::back,this));
    back->setScale(0.3);
    back->setPosition(Vec2(30,this->visibleSize.height-30));
    auto menu = Menu::create(back,NULL);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu);
    
    this->scoreLabel = CCLabelTTF::create("0","STCaiyun",40);
    this->scoreLabel->setPosition(Vec2(this->visibleSize.width/2,this->visibleSize.height-50));
    this->addChild(this->scoreLabel);
    

    this->player = Player::create(Vec2(this->visibleSize.width/2,100));
    this->addChild(player,100);
    
    auto touch = EventListenerTouchOneByOne::create();//单点触摸事件
    touch->onTouchBegan=CC_CALLBACK_2(GameScene::onTouchBegan,this);
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touch, this);//获取事件分发器，并绑定事件，并制定对象this.

    this->pmanager = PointManager::create();
    this->addChild(this->pmanager);
    this->pmanager->setPlayerWidth(this->player->getContentSize().width);
    this->pmanager->createPoint(this->player->getPosition());
    
    this->canspawn = 0;
    this->pr = this->player->getR();
    this->score = 0;
    

    return true;
}


void GameScene::back(){
    
    
    Director::getInstance()->replaceScene(HelloWorld::createScene());
}

bool GameScene::onTouchBegan(Touch*t,Event*e){
    
    this->player->changeAnPoint();//​ 返回player的可移动点的世界坐标用于检测
    GPoint* target = (GPoint*)this->getChildByTag(666);//获取白点对象

    if(target!=NULL){   //判断是否为空对象
        
        Vec2 pos = this->player->getActionPoint();  //获取player对象的坐标。
        float dis = pos.getDistance(target->getPosition());  //获取白点对象和player对象的距离
        float offset = (dis-(target->getR()+this->pr));   //获取（两者半径和）和 距离差
 
        if(offset<0){  //根据距离差判定游戏
            
            target->setTag(0);  //修改该白点对象的tag，防止妨碍下次白点对象的获取
            this->canspawn++;    //计数++
            this->score+= offset<=-28? 4:1;  //当player对象非常完美的落入到白点对象上时+4分，否则+1分
            if(this->canspawn!=1){
            
                 this->oldPoint->toFadeOut();//移除白点对象，由于第一生成不能立即移除
            }
                this->oldPoint = target;  //记录前一个白点对象
            
            this->scoreLabel->setString(__String::createWithFormat("%i",this->score)->getCString()); //修改显示分数
            this->pmanager->createPoint(this->player->getPosition());  //生成新的白点对象
            
            if(this->score%10==0){
                this->player->addSpeed(0.5);
            }
            
        }else{
            
            //DIE
            auto rm = RankManager::getInstance();
            int v1 = rm->getR("r1");
            int v2 = rm->getR("r2");
            int v3 = rm->getR("r3");
            
            if(this->score>v1){
                rm->saveScore("r1", this->score);
                rm->saveScore("r2", v1);
                rm->saveScore("r3", v2);
            }else if(this->score>v2){
                rm->saveScore("r2", this->score);
                rm->saveScore("r3", v2);
                
            }else if(this->score>v3){
                rm->saveScore("r3", this->score);
                
            }
            
            auto  gameover = GameOver::create(this->score);
            this->addChild(gameover,1000);
            auto daley = DelayTime::create(2);
            auto call = CallFunc::create(CC_CALLBACK_0(GameScene::back,this));
            auto seq = Sequence::create(daley,call, NULL);
            this->runAction(seq);
            
        }
    }

    return true;
}
